package com.weixing.mall.provider.controller.front;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weixing.mall.base.constant.GlobalConstant;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.api.model.vo.RoleVo;
import com.weixing.mall.provider.api.service.SysUserRpcService;
import com.weixing.mall.provider.model.query.FrontShipperReprotQuery;
import com.weixing.mall.provider.model.query.ShipperOrderQuery;
import com.weixing.mall.provider.model.vo.report.FrontShipperReprot;
import com.weixing.mall.provider.model.vo.report.customer.CustomerReportDiscountVo;
import com.weixing.mall.provider.service.IFrontShipperReprotService;
import com.weixing.mall.provider.service.IOrderService;
import com.weixing.mall.provider.service.impl.FrontShipperOrderServerImpl;
import com.weixing.mall.provider.util.EasyExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: mall-master
 * @description: 发货部前端报表
 * @author: wangfang
 * @create: 2021-11-10 07:38
 **/
@RestController
@RequestMapping("front/shipperReprot")
@Api(value = "前端:发货部报表模块",tags = "前端:发货部报表模块")
public class FrontShipperReportController extends BaseController {

    @Autowired
    private IFrontShipperReprotService reprotService;

    @Autowired
    private SysUserRpcService sysUserRpcService;

    @ApiOperation(value = "发货部订单列表")
    @GetMapping("dayReprot")
    public Result dayReprot(@SysInfo SysAuth sysAuth, FrontShipperReprotQuery reprotQuery){
        Long shipperId=  reprotService.getShippId(sysAuth);
        if(ObjectUtil.isNotNull(shipperId)){
            reprotQuery.setShipperId(shipperId);
        }
        return ResultUtil.success(reprotService.dayRepot(initMpPage(),reprotQuery));
    }

    @ApiOperation(value = "发货部订单列表导出")
    @GetMapping("dayReprotExprot")
    public void dayReprotExprot(@SysInfo SysAuth sysAuth, FrontShipperReprotQuery reprotQuery, HttpServletResponse response){
        Long shipperId=  reprotService.getShippId(sysAuth);
        if(ObjectUtil.isNotNull(shipperId)){
            reprotQuery.setShipperId(shipperId);
        }
        IPage<FrontShipperReprot>  pageList= reprotService.dayRepot(new Page(1,-1),reprotQuery);
        List<FrontShipperReprot> records = pageList.getRecords();
        try {
            EasyExcelUtil.writeExcel(response,FrontShipperReprot.class,records,"发货部订单日报");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    @ApiOperation(value = "发货部订单列表")
    @GetMapping("monthReport")
    public Result monthReport(@SysInfo SysAuth sysAuth,FrontShipperReprotQuery reprotQuery){
        Long shipperId=  reprotService.getShippId(sysAuth);
        if(ObjectUtil.isNotNull(shipperId)){
            reprotQuery.setShipperId(shipperId);
        }
        return ResultUtil.success(reprotService.monthRepot(initMpPage(),reprotQuery));
    }

    @ApiOperation(value = "发货部订单列表")
    @GetMapping("monthReportExport")
    public void monthReport(@SysInfo SysAuth sysAuth,FrontShipperReprotQuery reprotQuery,HttpServletResponse response){
        Long shipperId=  reprotService.getShippId(sysAuth);
        if(ObjectUtil.isNotNull(shipperId)){
            reprotQuery.setShipperId(shipperId);
        }
        IPage<FrontShipperReprot>  pageList=reprotService.monthRepot(new Page(1,-1),reprotQuery);

        List<FrontShipperReprot> records = pageList.getRecords();
        try {
            EasyExcelUtil.writeExcel(response,FrontShipperReprot.class,records,"发货部订单月报");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    @ApiOperation(value = "发货部日报合计")
    @GetMapping("dayRepotTotal")
    public Result dayRepotTotal(@SysInfo SysAuth sysAuth,FrontShipperReprotQuery reprotQuery){
        Long shipperId=  reprotService.getShippId(sysAuth);
        if(ObjectUtil.isNotNull(shipperId)){
            reprotQuery.setShipperId(shipperId);
        }
        return ResultUtil.success(reprotService.dayRepotTotal(reprotQuery));
    }

    @ApiOperation(value = "发货部订单列表")
    @GetMapping("monthRepotTotal")
    public Result monthRepotTotal(@SysInfo SysAuth sysAuth,FrontShipperReprotQuery reprotQuery){
        Long shipperId=  reprotService.getShippId(sysAuth);
        if(ObjectUtil.isNotNull(shipperId)){
            reprotQuery.setShipperId(shipperId);
        }
        return ResultUtil.success(reprotService.monthRepotTotal(reprotQuery));
    }

    @ApiOperation(value = "发货部订单列表")
    @GetMapping("total")
    public Result total(@SysInfo SysAuth sysAuth){
        List<RoleVo> list = sysUserRpcService.getRoleById(sysAuth.getUid()).getData();
        for (RoleVo vo : list) {
            if (vo.getRoleCode().equals(GlobalConstant.RoleCode.SHIPPING)) {
                return ResultUtil.success(reprotService.total(sysAuth.getUid()));
            }
        }
        return ResultUtil.success(reprotService.total(null));

    }
}
